package com.fourforfo.fourmall.product.dao;

import com.fourforfo.fourmall.product.entity.AttrAttrgroupRelationEntity;
import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.fourforfo.fourmall.product.vo.ProductItemVO;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;

import java.util.List;

/**
 * 属性&属性分组关联
 * 
 * @author fourforfo
 * @email fourforfo@gmail.com
 * @date 2022-06-15 21:04:46
 */
@Mapper
public interface AttrAttrgroupRelationDao extends BaseMapper<AttrAttrgroupRelationEntity> {


    void deleteBatchRelation(@Param("entities") List<AttrAttrgroupRelationEntity> entities);

    void deleteByAttrIds(@Param("attrIds") List<Long> attrIds);

    void deleteByAttrGroupIds(@Param("attrGroupIds") List<Long> attrGroupIds);

    /**
     * 根据spuId获取attrId，attrName，attrValue，attrGroupId，attrGroupName
     * 1.根据spuId从pms_product_attr_value表中获取所有attrId属性
     *   SELECT attr_id FROM pms_product_attr_value AS e WHERE e.spu_id=13 AND e.attr_id=d.attr_id
     *
     * 2.根据第1步获取的attrIds从pms_attr_attrgroup_relation表中查出所有attr_group_id并按attr_group_id分组
     *   SELECT d.attr_group_id FROM pms_attr_attrgroup_relation AS d
     *     WHERE EXISTS(第1步的SQL) AND d.attr_group_id=c.attr_group_id GROUP BY c.attr_group_id
     *
     * 3.根据第2步获取的attr_group_id从表pms_attr_group 查出所有符合条件的attr_group_id和attr_group_name属性
     *   SELECT c.attr_group_id,c.attr_group_name from pms_attr_group AS c WHERE EXISTS(第2步的SQL)
     *
     * 4.将表pms_attr_attrgroup_relation 与第3步获取的attr_group_id和attr_group_name数据根据attr_group_id进行left join操作
     *   SELECT a.attr_id,a.attr_group_id,b.attr_group_name
     *     FROM pms_attr_attrgroup_relation AS a
     *     LEFT JOIN ( [第3步的SQL] ) AS b ON a.attr_group_id=b.attr_group_id;
     *
     * 5.将表`pms_attr_attrgroup_relation` 与第4步获取的`attr_id`，`attr_group_id`和`attr_group_name`数据根据`attr_id`进行`left join`操作
     *   SELECT x.attr_id,x.attr_name,x.attr_value,y.attr_group_id,y.attr_group_name
     * 	   FROM pms_product_attr_value AS x
     * 	   LEFT JOIN (第4步的SQL) AS y
     *     ON x.attr_id=y.attr_id;
     */
    List<ProductItemVO.SpuItemBaseAttrVO> listAttrGroupAndAttr(@Param("spuId") Long spuId);
}
